package cn.gov.msa.ws;

import java.util.List;

import org.springframework.context.ApplicationContext;

import com.github.pagehelper.PageInfo;

import cn.gov.msa.model.DeclareItem;
import cn.gov.msa.model.Reserve;
import cn.gov.msa.model.Rule;
import cn.gov.msa.model.RuleTime;
import cn.gov.msa.service.DictionaryDetailService;
import cn.gov.msa.service.RuleService;
import cn.gov.msa.util.GsonHelper;
import cn.gov.msa.util.InfoObjUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**   
* @Title: ReserveService.java 
* @Package cn.gov.msa.ws 
* @Description: 预约查询(用一句话描述该文件做什么) 
* @author 冷云鹏  
* @date 2018年3月27日 上午10:08:56 
*/
public class ReserveService {
	
	public static String setData(ApplicationContext ctx, String params) throws Exception {
		String result = "";
		
	    JSONObject jsonResult = new JSONObject();
	    cn.gov.msa.service.ReserveService reserveService = (cn.gov.msa.service.ReserveService) ctx.getBean("ReserveService");  				        
		JSONObject jsonParams = JSONObject.fromObject(params);
		int pageNum = 1;
		int pageSize = 10;
		if(jsonParams.has("pageNum"))
		{
			pageNum = jsonParams.getInt("pageNum");
			jsonParams.remove("pageNum");
		}
		if(jsonParams.has("pageSize"))
		{
			pageSize = jsonParams.getInt("pageSize");
			jsonParams.remove("pageSize");
		}	
		Reserve reserve = GsonHelper.convertEntity(jsonParams.toString(), Reserve.class);
		List<Reserve> reserveList=reserveService.selectData(pageNum, pageSize, reserve);
	    JSONArray jaResult = JSONArray.fromObject(reserveList);
	    //获取分页信息
        PageInfo page = new PageInfo(reserveList);
        
        jsonResult.put("total", page.getTotal());
		jsonResult.put("rows", jaResult);	
		
		return jsonResult.toString();
	}
	
	public static String setItem(ApplicationContext ctx, String params) throws Exception {
		String result = "";
		String info="";
	    JSONObject jsonResult = new JSONObject();
	    cn.gov.msa.service.RuleService ruleService = (cn.gov.msa.service.RuleService) ctx.getBean("RuleService");  				        
		
	    List<Rule> reserveList = ruleService.selectDict(JSONObject.fromObject(params).optString("orgcode"));
	    
	    JSONArray ja = JSONArray.fromObject(reserveList);				
		
		result = reserveList.size() > 0 ? "1" : "0";
		info = reserveList.size() > 0 ? InfoObjUtil.Success_select_OK : InfoObjUtil.Error_NODATA;
		
		jsonResult.put("datas", ja);											
		jsonResult.put("result", result);
		jsonResult.put("info", info);
       			
		return jsonResult.toString();
	}
	
	public static String deleteDate(ApplicationContext ctx, String params) throws Exception {
		String result = "";
	    JSONObject jsonResult = new JSONObject();
	    cn.gov.msa.service.ReserveService reserveService = (cn.gov.msa.service.ReserveService) ctx.getBean("ReserveService");  				        
		JSONObject jsonParams = JSONObject.fromObject(params);
		Reserve reserve = GsonHelper.convertEntity(jsonParams.toString(), Reserve.class);
		reserveService.deleteData(reserve);
       
        jsonResult.put("result", "1");
			
		return jsonResult.toString();
	}
	
	public static String insertData(ApplicationContext ctx, String params) throws Exception{		
		String result = "";
		String info = "";
		JSONObject jsonResult = new JSONObject();
		JSONObject jsonParams = JSONObject.fromObject(params);
		Reserve reserve = GsonHelper.convertEntity(jsonParams.toString(), Reserve.class);
		cn.gov.msa.service.ReserveService reserveService = (cn.gov.msa.service.ReserveService) ctx.getBean("ReserveService");
		RuleService ruleService = (RuleService) ctx.getBean("RuleService");
		Rule rule = new Rule();
		rule.setItemid(reserve.getITEMID());
		rule.setOrgcode(reserve.getORGCODE());
		Reserve reserve1 = new Reserve();
		reserve1.setITEMID(reserve.getITEMID());
		reserve1.setORGCODE(reserve.getORGCODE());
		reserve1.setRECORDDATE(reserve.getRECORDDATE());
		List<Rule> ruleList = ruleService.selectData(1, 10, rule);
		// 判断是否开启预约
		if (ruleList.size() == 0) {
			result = "2";
			info = "该项目没有开通预约";
		} else if (ruleList.get(0).getIspushlish().equals("0")) {
			result = "2";
			info = "该项目没有开通预约";
		} else {
			// 获取预约人数最大值
			// int max=ruleList.get(0).getReservenum();
			List<RuleTime> ruleTimeList = ruleService.getMaxNum(ruleList.get(0).getRuleid(), reserve.getRECORDPERIOD());
			if (ruleTimeList.size() == 0) {
				result = "2";
				info = "该时间段没有开通预约";
			} else {
				int max = ruleTimeList.get(0).getReservenum();
				List<Reserve> maxList = reserveService.selectData(reserve1);
				int max1 = maxList.size();
				if (max1 >= max) {
					result = "2";
					info = "超过预约最大人数";
				} else {
					String count = reserveService.check(reserve);
					if ("0".equals(count)) {
						// 当天预约数
						reserve1.setORGCODE(null);
						reserve1.setITEMID(null);
						List<Reserve> maxList1 = reserveService.selectData(reserve1);
						// 预约单号
						int no = maxList1.size() + 1;
						String no1 = String.format("%03d", no);
						String bespeakNo = reserve.getORGCODE() + reserve.getRECORDDATE() + no1;
						bespeakNo = bespeakNo.replace("-", "");
						System.err.println(bespeakNo);
						reserve.setBESPEAKNO(bespeakNo);
						// insert
						reserveService.insertData(reserve);
						result = "1";
						info = "单号为:<span style='color:red;'><strong>" + bespeakNo + "</strong></span>";
					} else {
						result = "2";
						info = "当天已预约该项目";
					}
				}
			}
		}

		jsonResult.put("result", result);
		jsonResult.put("info", info);
		return jsonResult.toString();		
	}
		
	public static String getNum(ApplicationContext ctx, String params) throws Exception{
		String result = "1";
		String info="成功获取剩余人数";
		JSONObject jsonResult = new JSONObject();
		JSONObject jsonParams = JSONObject.fromObject(params);
		Reserve reserve = GsonHelper.convertEntity(jsonParams.getString("reserve"), Reserve.class);
		String times = jsonParams.getString("times");
		times = times.substring(1,times.length() - 1);
		times = times.substring(0,times.length() - 1);
		times= times.replaceAll("\"", "");
		cn.gov.msa.service.ReserveService reserveService = (cn.gov.msa.service.ReserveService) ctx.getBean("ReserveService");
		String datas = reserveService.getNum(reserve, times.split(","));
		jsonResult.put("datas", datas);											
		jsonResult.put("result", result);
		jsonResult.put("info", info);
       	return jsonResult.toString();
	}
}
